home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / gnus / nnml.el.z / nnml.el
Encoding:
Text File  |  1998-05-21  |  27.1 KB  |  818 lines

  1. ;;; nnml.el --- mail spool access for Gnus
  2. ;; Copyright (C) 1995,96,97 Free Software Foundation, Inc.
  3.  
  4. ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
  5. ;;     Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
  6. ;; Keywords: news, mail
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  22. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. ;; Boston, MA 02111-1307, USA.
  24.  
  25. ;;; Commentary:
  26.  
  27. ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
  28. ;; For an overview of what the interface functions do, please see the
  29. ;; Gnus sources.
  30.  
  31. ;;; Code:
  32.  
  33. (require 'nnheader)
  34. (require 'nnmail)
  35. (require 'nnoo)
  36. (eval-when-compile (require 'cl))
  37.  
  38. (nnoo-declare nnml)
  39.  
  40. (defvoo nnml-directory message-directory
  41.   "Spool directory for the nnml mail backend.")
  42.  
  43. (defvoo nnml-active-file
  44.   (concat (file-name-as-directory nnml-directory) "active")
  45.   "Mail active file.")
  46.  
  47. (defvoo nnml-newsgroups-file
  48.   (concat (file-name-as-directory nnml-directory) "newsgroups")
  49.   "Mail newsgroups description file.")
  50.  
  51. (defvoo nnml-get-new-mail t
  52.   "If non-nil, nnml will check the incoming mail file and split the mail.")
  53.  
  54. (defvoo nnml-nov-is-evil nil
  55.   "If non-nil, Gnus will never generate and use nov databases for mail groups.
  56. Using nov databases will speed up header fetching considerably.
  57. This variable shouldn't be flipped much.  If you have, for some reason,
  58. set this to t, and want to set it to nil again, you should always run
  59. the `nnml-generate-nov-databases' command.  The function will go
  60. through all nnml directories and generate nov databases for them
  61. all.  This may very well take some time.")
  62.  
  63. (defvoo nnml-prepare-save-mail-hook nil
  64.   "Hook run narrowed to an article before saving.")
  65.  
  66. (defvoo nnml-inhibit-expiry nil
  67.   "If non-nil, inhibit expiry.")
  68.  
  69.  
  70.  
  71.  
  72. (defconst nnml-version "nnml 1.0"
  73.   "nnml version.")
  74.  
  75. (defvoo nnml-nov-file-name ".overview")
  76.  
  77. (defvoo nnml-current-directory nil)
  78. (defvoo nnml-current-group nil)
  79. (defvoo nnml-status-string "")
  80. (defvoo nnml-nov-buffer-alist nil)
  81. (defvoo nnml-group-alist nil)
  82. (defvoo nnml-active-timestamp nil)
  83. (defvoo nnml-article-file-alist nil)
  84.  
  85. (defvoo nnml-generate-active-function 'nnml-generate-active-info)
  86.  
  87.  
  88.  
  89. ;;; Interface functions.
  90.  
  91. (nnoo-define-basics nnml)
  92.  
  93. (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
  94.   (when (nnml-possibly-change-directory group server)
  95.     (save-excursion
  96.       (set-buffer nntp-server-buffer)
  97.       (erase-buffer)
  98.       (let ((file nil)
  99.         (number (length sequence))
  100.         (count 0)
  101.         ;; 1997/8/12 by MORIOKA Tomohiko
  102.         ;;    for XEmacs/mule.
  103.         (pathname-coding-system 'binary)
  104.         beg article)
  105.     (if (stringp (car sequence))
  106.         'headers
  107.       (if (nnml-retrieve-headers-with-nov sequence fetch-old)
  108.           'nov
  109.         (while sequence
  110.           (setq article (car sequence))
  111.           (setq file (nnml-article-to-file article))
  112.           (when (and file
  113.              (file-exists-p file)
  114.              (not (file-directory-p file)))
  115.         (insert (format "221 %d Article retrieved.\n" article))
  116.         (setq beg (point))
  117.         (nnheader-insert-head file)
  118.         (goto-char beg)
  119.         (if (search-forward "\n\n" nil t)
  120.             (forward-char -1)
  121.           (goto-char (point-max))
  122.           (insert "\n\n"))
  123.         (insert ".\n")
  124.         (delete-region (point) (point-max)))
  125.           (setq sequence (cdr sequence))
  126.           (setq count (1+ count))
  127.           (and (numberp nnmail-large-newsgroup)
  128.            (> number nnmail-large-newsgroup)
  129.            (zerop (% count 20))
  130.            (nnheader-message 6 "nnml: Receiving headers... %d%%"
  131.                      (/ (* count 100) number))))
  132.  
  133.         (and (numberp nnmail-large-newsgroup)
  134.          (> number nnmail-large-newsgroup)
  135.          (nnheader-message 6 "nnml: Receiving headers...done"))
  136.  
  137.         (nnheader-fold-continuation-lines)
  138.         'headers))))))
  139.  
  140. (deffoo nnml-open-server (server &optional defs)
  141.   (nnoo-change-server 'nnml server defs)
  142.   (when (not (file-exists-p nnml-directory))
  143.     (condition-case ()
  144.     (make-directory nnml-directory t)
  145.       (error)))
  146.   (cond
  147.    ((not (file-exists-p nnml-directory))
  148.     (nnml-close-server)
  149.     (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
  150.    ((not (file-directory-p (file-truename nnml-directory)))
  151.     (nnml-close-server)
  152.     (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
  153.    (t
  154.     (nnheader-report 'nnml "Opened server %s using directory %s"
  155.              server nnml-directory)
  156.     t)))
  157.  
  158. (defun nnml-request-regenerate (server)
  159.   (nnml-possibly-change-directory nil server)
  160.   (nnml-generate-nov-databases)
  161.   t)
  162.  
  163. (deffoo nnml-request-article (id &optional group server buffer)
  164.   (nnml-possibly-change-directory group server)
  165.   (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
  166.      ;; 1997/8/12 by MORIOKA Tomohiko
  167.      ;;    for XEmacs/mule.
  168.      (pathname-coding-system 'binary)
  169.      path gpath group-num)
  170.     (if (stringp id)
  171.     (when (and (setq group-num (nnml-find-group-number id))
  172.            (cdr
  173.             (assq (cdr group-num)
  174.               (nnheader-article-to-file-alist
  175.                (setq gpath
  176.                  (nnmail-group-pathname
  177.                   (car group-num)
  178.                   nnml-directory))))))
  179.       (setq path (concat gpath (int-to-string (cdr group-num)))))
  180.       (setq path (nnml-article-to-file id)))
  181.     (cond
  182.      ((not path)
  183.       (nnheader-report 'nnml "No such article: %s" id))
  184.      ((not (file-exists-p path))
  185.       (nnheader-report 'nnml "No such file: %s" path))
  186.      ((file-directory-p path)
  187.       (nnheader-report 'nnml "File is a directory: %s" path))
  188.      ((not (save-excursion (nnmail-find-file path)))
  189.       (nnheader-report 'nnml "Couldn't read file: %s" path))
  190.      (t
  191.       (nnheader-report 'nnml "Article %s retrieved" id)
  192.       ;; We return the article number.
  193.       (cons (if group-num (car group-num) group)
  194.         (string-to-int (file-name-nondirectory path)))))))
  195.  
  196. (deffoo nnml-request-group (group &optional server dont-check)
  197.   ;; 1997/8/12 by MORIOKA Tomohiko
  198.   ;;    for XEmacs/mule.
  199.   (let ((pathname-coding-system 'binary))
  200.     (cond
  201.      ((not (nnml-possibly-change-directory group server))
  202.       (nnheader-report 'nnml "Invalid group (no such directory)"))
  203.      ((not (file-exists-p nnml-current-directory))
  204.       (nnheader-report 'nnml "Directory %s does not exist"
  205.                nnml-current-directory))
  206.      ((not (file-directory-p nnml-current-directory))
  207.       (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
  208.      (dont-check
  209.       (nnheader-report 'nnml "Group %s selected" group)
  210.       t)
  211.      (t
  212.       (nnheader-re-read-dir nnml-current-directory)
  213.       (nnmail-activate 'nnml)
  214.       (let ((active (nth 1 (assoc group nnml-group-alist))))
  215.     (if (not active)
  216.         (nnheader-report 'nnml "No such group: %s" group)
  217.       (nnheader-report 'nnml "Selected group %s" group)
  218.       (nnheader-insert "211 %d %d %d %s\n"
  219.                (max (1+ (- (cdr active) (car active))) 0)
  220.                (car active) (cdr active) group)))))))
  221.  
  222. (deffoo nnml-request-scan (&optional group server)
  223.   (setq nnml-article-file-alist nil)
  224.   (nnml-possibly-change-directory group server)
  225.   (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
  226.  
  227. (deffoo nnml-close-group (group &optional server)
  228.   (setq nnml-article-file-alist nil)
  229.   t)
  230.  
  231. (deffoo nnml-request-create-group (group &optional server args)
  232.   (nnmail-activate 'nnml)
  233.   (unless (assoc group nnml-group-alist)
  234.     (let (active)
  235.       (push (list group (setq active (cons 1 0)))
  236.         nnml-group-alist)
  237.       (nnml-possibly-create-directory group)
  238.       (nnml-possibly-change-directory group server)
  239.       (let ((articles (nnheader-directory-articles nnml-current-directory)))
  240.     (when articles
  241.       (setcar active (apply 'min articles))
  242.       (setcdr active (apply 'max articles))))
  243.       (nnmail-save-active nnml-group-alist nnml-active-file)))
  244.   t)
  245.  
  246. (deffoo nnml-request-list (&optional server)
  247.   (save-excursion
  248.     ;; 1997/8/12 by MORIOKA Tomohiko
  249.     ;;    for XEmacs/mule.
  250.     (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
  251.       (pathname-coding-system 'binary)) ; for XEmacs/mule
  252.       (nnmail-find-file nnml-active-file)
  253.       )
  254.     (setq nnml-group-alist (nnmail-get-active))
  255.     t))
  256.  
  257. (deffoo nnml-request-newgroups (date &optional server)
  258.   (nnml-request-list server))
  259.  
  260. (deffoo nnml-request-list-newsgroups (&optional server)
  261.   (save-excursion
  262.     (nnmail-find-file nnml-newsgroups-file)))
  263.  
  264. (deffoo nnml-request-expire-articles (articles group
  265.                            &optional server force)
  266.   (nnml-possibly-change-directory group server)
  267.   (let* ((active-articles
  268.       (nnheader-directory-articles nnml-current-directory))
  269.      (is-old t)
  270.      article rest mod-time number)
  271.     (nnmail-activate 'nnml)
  272.  
  273.     (while (and articles is-old)
  274.       (when (setq article (nnml-article-to-file (setq number (pop articles))))
  275.     (when (setq mod-time (nth 5 (file-attributes article)))
  276.       (if (and (nnml-deletable-article-p group number)
  277.            (setq is-old
  278.              (nnmail-expired-article-p group mod-time force
  279.                            nnml-inhibit-expiry)))
  280.           (progn
  281.         (nnheader-message 5 "Deleting article %s in %s"
  282.                   article group)
  283.         (condition-case ()
  284.             (funcall nnmail-delete-file-function article)
  285.           (file-error
  286.            (push number rest)))
  287.         (setq active-articles (delq number active-articles))
  288.         (nnml-nov-delete-article group number))
  289.         (push number rest)))))
  290.     (let ((active (nth 1 (assoc group nnml-group-alist))))
  291.       (when active
  292.     (setcar active (or (and active-articles
  293.                 (apply 'min active-articles))
  294.                (1+ (cdr active)))))
  295.       (nnmail-save-active nnml-group-alist nnml-active-file))
  296.     (nnml-save-nov)
  297.     (nconc rest articles)))
  298.  
  299. (deffoo nnml-request-move-article
  300.   (article group server accept-form &optional last)
  301.   (let ((buf (get-buffer-create " *nnml move*"))
  302.     result)
  303.     (nnml-possibly-change-directory group server)
  304.     (nnml-update-file-alist)
  305.     (and
  306.      (nnml-deletable-article-p group article)
  307.      (nnml-request-article article group server)
  308.      (save-excursion
  309.        (set-buffer buf)
  310.        (insert-buffer-substring nntp-server-buffer)
  311.        (setq result (eval accept-form))
  312.        (kill-buffer (current-buffer))
  313.        result)
  314.      (progn
  315.        (nnml-possibly-change-directory group server)
  316.        (condition-case ()
  317.        (funcall nnmail-delete-file-function
  318.             (nnml-article-to-file  article))
  319.      (file-error nil))
  320.        (nnml-nov-delete-article group article)
  321.        (when last
  322.      (nnml-save-nov)
  323.      (nnmail-save-active nnml-group-alist nnml-active-file))))
  324.     result))
  325.  
  326. (deffoo nnml-request-accept-article (group &optional server last)
  327.   (nnml-possibly-change-directory group server)
  328.   (nnmail-check-syntax)
  329.   (let (result)
  330.     (when nnmail-cache-accepted-message-ids
  331.       (nnmail-cache-insert (nnmail-fetch-field "message-id")))
  332.     (if (stringp group)
  333.     (and
  334.      (nnmail-activate 'nnml)
  335.      (setq result (car (nnml-save-mail
  336.                 (list (cons group (nnml-active-number group))))))
  337.      (progn
  338.        (nnmail-save-active nnml-group-alist nnml-active-file)
  339.        (and last (nnml-save-nov))))
  340.       (and
  341.        (nnmail-activate 'nnml)
  342.        (if (and (not (setq result (nnmail-article-group 'nnml-active-number)))
  343.         (yes-or-no-p "Moved to `junk' group; delete article? "))
  344.        (setq result 'junk)
  345.      (setq result (car (nnml-save-mail result))))
  346.        (when last
  347.      (nnmail-save-active nnml-group-alist nnml-active-file)
  348.      (when nnmail-cache-accepted-message-ids
  349.        (nnmail-cache-close))
  350.      (nnml-save-nov))))
  351.     result))
  352.  
  353. (deffoo nnml-request-replace-article (article group buffer)
  354.   (nnml-possibly-change-directory group)
  355.   (save-excursion
  356.     (set-buffer buffer)
  357.     (nnml-possibly-create-directory group)
  358.     (let ((chars (nnmail-insert-lines))
  359.       (art (concat (int-to-string article) "\t"))
  360.       headers)
  361.       (when (condition-case ()
  362.         (progn
  363.           (nnmail-write-region
  364.            (point-min) (point-max)
  365.            (or (nnml-article-to-file article)
  366.                (concat nnml-current-directory
  367.                    (int-to-string article)))
  368.            nil (if (nnheader-be-verbose 5) nil 'nomesg))
  369.           t)
  370.           (error nil))
  371.     (setq headers (nnml-parse-head chars article))
  372.     ;; Replace the NOV line in the NOV file.
  373.     (save-excursion
  374.       (set-buffer (nnml-open-nov group))
  375.       (goto-char (point-min))
  376.       (if (or (looking-at art)
  377.           (search-forward (concat "\n" art) nil t))
  378.           ;; Delete the old NOV line.
  379.           (delete-region (progn (beginning-of-line) (point))
  380.                  (progn (forward-line 1) (point)))
  381.         ;; The line isn't here, so we have to find out where
  382.         ;; we should insert it.  (This situation should never
  383.         ;; occur, but one likes to make sure...)
  384.         (while (and (looking-at "[0-9]+\t")
  385.             (< (string-to-int
  386.                 (buffer-substring
  387.                  (match-beginning 0) (match-end 0)))
  388.                article)
  389.             (zerop (forward-line 1)))))
  390.       (beginning-of-line)
  391.       (nnheader-insert-nov headers)
  392.       (nnml-save-nov)
  393.       t)))))
  394.  
  395. (deffoo nnml-request-delete-group (group &optional force server)
  396.   (nnml-possibly-change-directory group server)
  397.   (when force
  398.     ;; Delete all articles in GROUP.
  399.     (let ((articles
  400.        (directory-files
  401.         nnml-current-directory t
  402.         (concat nnheader-numerical-short-files
  403.             "\\|" (regexp-quote nnml-nov-file-name) "$")))
  404.       article)
  405.       (while articles
  406.     (setq article (pop articles))
  407.     (when (file-writable-p article)
  408.       (nnheader-message 5 "Deleting article %s in %s..." article group)
  409.       (funcall nnmail-delete-file-function article))))
  410.     ;; Try to delete the directory itself.
  411.     (condition-case ()
  412.     (delete-directory nnml-current-directory)
  413.       (error nil)))
  414.   ;; Remove the group from all structures.
  415.   (setq nnml-group-alist
  416.     (delq (assoc group nnml-group-alist) nnml-group-alist)
  417.     nnml-current-group nil
  418.     nnml-current-directory nil)
  419.   ;; Save the active file.
  420.   (nnmail-save-active nnml-group-alist nnml-active-file)
  421.   t)
  422.  
  423. (deffoo nnml-request-rename-group (group new-name &optional server)
  424.   (nnml-possibly-change-directory group server)
  425.   (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
  426.     (old-dir (nnmail-group-pathname group nnml-directory)))
  427.     (when (condition-case ()
  428.           (progn
  429.         (make-directory new-dir t)
  430.         t)
  431.         (error nil))
  432.       ;; We move the articles file by file instead of renaming
  433.       ;; the directory -- there may be subgroups in this group.
  434.       ;; One might be more clever, I guess.
  435.       (let ((files (nnheader-article-to-file-alist old-dir)))
  436.     (while files
  437.       (rename-file
  438.        (concat old-dir (cdar files))
  439.        (concat new-dir (cdar files)))
  440.       (pop files)))
  441.       ;; Move .overview file.
  442.       (let ((overview (concat old-dir nnml-nov-file-name)))
  443.     (when (file-exists-p overview)
  444.       (rename-file overview (concat new-dir nnml-nov-file-name))))
  445.       (when (<= (length (directory-files old-dir)) 2)
  446.     (condition-case ()
  447.         (delete-directory old-dir)
  448.       (error nil)))
  449.       ;; That went ok, so we change the internal structures.
  450.       (let ((entry (assoc group nnml-group-alist)))
  451.     (when entry
  452.       (setcar entry new-name))
  453.     (setq nnml-current-directory nil
  454.           nnml-current-group nil)
  455.     ;; Save the new group alist.
  456.     (nnmail-save-active nnml-group-alist nnml-active-file)
  457.     t))))
  458.  
  459. (deffoo nnml-set-status (article name value &optional group server)
  460.   (nnml-possibly-change-directory group server)
  461.   (let ((file (nnml-article-to-file article)))
  462.     (cond
  463.      ((not (file-exists-p file))
  464.       (nnheader-report 'nnml "File %s does not exist" file))
  465.      (t
  466.       (nnheader-temp-write file
  467.     (nnheader-insert-file-contents file)
  468.     (nnmail-replace-status name value))
  469.       t))))
  470.  
  471.  
  472. ;;; Internal functions.
  473.  
  474. (defun nnml-article-to-file (article)
  475.   (nnml-update-file-alist)
  476.   (let (file)
  477.     (if (setq file (cdr (assq article nnml-article-file-alist)))
  478.     (concat nnml-current-directory file)
  479.       ;; Just to make sure nothing went wrong when reading over NFS --
  480.       ;; check once more.
  481.       (when (file-exists-p
  482.          (setq file (concat nnml-current-directory "/"
  483.                 (number-to-string article))))
  484.     (nnml-update-file-alist t)
  485.     file))))
  486.  
  487. (defun nnml-deletable-article-p (group article)
  488.   "Say whether ARTICLE in GROUP can be deleted."
  489.   (let (path)
  490.     (when (setq path (nnml-article-to-file article))
  491.       (when (file-writable-p path)
  492.     (or (not nnmail-keep-last-article)
  493.         (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
  494.              article)))))))
  495.  
  496. ;; Find an article number in the current group given the Message-ID.
  497. (defun nnml-find-group-number (id)
  498.   (save-excursion
  499.     (set-buffer (get-buffer-create " *nnml id*"))
  500.     (buffer-disable-undo (current-buffer))
  501.     (let ((alist nnml-group-alist)
  502.       number)
  503.       ;; We want to look through all .overview files, but we want to
  504.       ;; start with the one in the current directory.  It seems most
  505.       ;; likely that the article we are looking for is in that group.
  506.       (if (setq number (nnml-find-id nnml-current-group id))
  507.       (cons nnml-current-group number)
  508.     ;; It wasn't there, so we look through the other groups as well.
  509.     (while (and (not number)
  510.             alist)
  511.       (or (string= (caar alist) nnml-current-group)
  512.           (setq number (nnml-find-id (caar alist) id)))
  513.       (or number
  514.           (setq alist (cdr alist))))
  515.     (and number
  516.          (cons (caar alist) number))))))
  517.  
  518. (defun nnml-find-id (group id)
  519.   (erase-buffer)
  520.   (let ((nov (concat (nnmail-group-pathname group nnml-directory)
  521.              nnml-nov-file-name))
  522.     number found)
  523.     (when (file-exists-p nov)
  524.       (nnheader-insert-file-contents nov)
  525.       (while (and (not found)
  526.           (search-forward id nil t)) ; We find the ID.
  527.     ;; And the id is in the fourth field.
  528.     (if (not (and (search-backward "\t" nil t 4)
  529.               (not (search-backward"\t" (gnus-point-at-bol) t))))
  530.         (forward-line 1)
  531.       (beginning-of-line)
  532.       (setq found t)
  533.       ;; We return the article number.
  534.       (setq number
  535.         (condition-case ()
  536.             (read (current-buffer))
  537.           (error nil)))))
  538.       number)))
  539.  
  540. (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
  541.   (if (or gnus-nov-is-evil nnml-nov-is-evil)
  542.       nil
  543.     (let ((nov (concat nnml-current-directory nnml-nov-file-name)))
  544.       (when (file-exists-p nov)
  545.     (save-excursion
  546.       (set-buffer nntp-server-buffer)
  547.       (erase-buffer)
  548.       (nnheader-insert-file-contents nov)
  549.       (if (and fetch-old
  550.            (not (numberp fetch-old)))
  551.           t                ; Don't remove anything.
  552.         (nnheader-nov-delete-outside-range
  553.          (if fetch-old (max 1 (- (car articles) fetch-old))
  554.            (car articles))
  555.          (car (last articles)))
  556.         t))))))
  557.  
  558. (defun nnml-possibly-change-directory (group &optional server)
  559.   (when (and server
  560.          (not (nnml-server-opened server)))
  561.     (nnml-open-server server))
  562.   (if (not group)
  563.       t
  564.     (let ((pathname (nnmail-group-pathname group nnml-directory))
  565.       ;; 1997/8/14 by MORIOKA Tomohiko
  566.       ;;    for XEmacs/mule.
  567.       (pathname-coding-system 'binary))
  568.       (when (not (equal pathname nnml-current-directory))
  569.     (setq nnml-current-directory pathname
  570.           nnml-current-group group
  571.           nnml-article-file-alist nil))
  572.       (file-exists-p nnml-current-directory))))
  573.  
  574. (defun nnml-possibly-create-directory (group)
  575.   (let (dir dirs)
  576.     (setq dir (nnmail-group-pathname group nnml-directory))
  577.     (while (not (file-directory-p dir))
  578.       (push dir dirs)
  579.       (setq dir (file-name-directory (directory-file-name dir))))
  580.     (while dirs
  581.       (make-directory (directory-file-name (car dirs)))
  582.       (nnheader-message 5 "Creating mail directory %s" (car dirs))
  583.       (setq dirs (cdr dirs)))))
  584.  
  585. (defun nnml-save-mail (group-art)
  586.   "Called narrowed to an article."
  587.   (let (chars headers)
  588.     (setq chars (nnmail-insert-lines))
  589.     (nnmail-insert-xref group-art)
  590.     (run-hooks 'nnmail-prepare-save-mail-hook)
  591.     (run-hooks 'nnml-prepare-save-mail-hook)
  592.     (goto-char (point-min))
  593.     (while (looking-at "From ")
  594.       (replace-match "X-From-Line: ")
  595.       (forward-line 1))
  596.     ;; We save the article in all the groups it belongs in.
  597.     (let ((ga group-art)
  598.       first)
  599.       (while ga
  600.     (nnml-possibly-create-directory (caar ga))
  601.     (let ((file (concat (nnmail-group-pathname
  602.                  (caar ga) nnml-directory)
  603.                 (int-to-string (cdar ga)))))
  604.       (if first
  605.           ;; It was already saved, so we just make a hard link.
  606.           (funcall nnmail-crosspost-link-function first file t)
  607.         ;; Save the article.
  608.         (nnmail-write-region (point-min) (point-max) file nil
  609.                  (if (nnheader-be-verbose 5) nil 'nomesg))
  610.         (setq first file)))
  611.     (setq ga (cdr ga))))
  612.     ;; Generate a nov line for this article.  We generate the nov
  613.     ;; line after saving, because nov generation destroys the
  614.     ;; header.
  615.     (setq headers (nnml-parse-head chars))
  616.     ;; Output the nov line to all nov databases that should have it.
  617.     (let ((ga group-art))
  618.       (while ga
  619.     (nnml-add-nov (caar ga) (cdar ga) headers)
  620.     (setq ga (cdr ga))))
  621.     group-art))
  622.  
  623. (defun nnml-active-number (group)
  624.   "Compute the next article number in GROUP."
  625.   (let ((active (cadr (assoc group nnml-group-alist))))
  626.     ;; The group wasn't known to nnml, so we just create an active
  627.     ;; entry for it.
  628.     (unless active
  629.       ;; Perhaps the active file was corrupt?  See whether
  630.       ;; there are any articles in this group.
  631.       (nnml-possibly-create-directory group)
  632.       (nnml-possibly-change-directory group)
  633.       (unless nnml-article-file-alist
  634.     (setq nnml-article-file-alist
  635.           (sort
  636.            (nnheader-article-to-file-alist nnml-current-directory)
  637.            (lambda (a1 a2) (< (car a1) (car a2))))))
  638.       (setq active
  639.         (if nnml-article-file-alist
  640.         (cons (caar nnml-article-file-alist)
  641.               (caar (last nnml-article-file-alist)))
  642.           (cons 1 0)))
  643.       (push (list group active) nnml-group-alist))
  644.     (setcdr active (1+ (cdr active)))
  645.     (while (file-exists-p
  646.         (concat (nnmail-group-pathname group nnml-directory)
  647.             (int-to-string (cdr active))))
  648.       (setcdr active (1+ (cdr active))))
  649.     (cdr active)))
  650.  
  651. (defun nnml-add-nov (group article headers)
  652.   "Add a nov line for the GROUP base."
  653.   (save-excursion
  654.     (set-buffer (nnml-open-nov group))
  655.     (goto-char (point-max))
  656.     (mail-header-set-number headers article)
  657.     (nnheader-insert-nov headers)))
  658.  
  659. (defsubst nnml-header-value ()
  660.   (buffer-substring (match-end 0) (progn (end-of-line) (point))))
  661.  
  662. (defun nnml-parse-head (chars &optional number)
  663.   "Parse the head of the current buffer."
  664.   (save-excursion
  665.     (save-restriction
  666.       (goto-char (point-min))
  667.       (narrow-to-region
  668.        (point)
  669.        (1- (or (search-forward "\n\n" nil t) (point-max))))
  670.       ;; Fold continuation lines.
  671.       (goto-char (point-min))
  672.       (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
  673.     (replace-match " " t t))
  674.       ;; Remove any tabs; they are too confusing.
  675.       (subst-char-in-region (point-min) (point-max) ?\t ? )
  676.       (let ((headers (nnheader-parse-head t)))
  677.     (mail-header-set-chars headers chars)
  678.     (mail-header-set-number headers number)
  679.     headers))))
  680.  
  681. (defun nnml-open-nov (group)
  682.   (or (cdr (assoc group nnml-nov-buffer-alist))
  683.       (let ((buffer (nnheader-find-file-noselect
  684.              (concat (nnmail-group-pathname group nnml-directory)
  685.                  nnml-nov-file-name))))
  686.     (save-excursion
  687.       (set-buffer buffer)
  688.       (buffer-disable-undo (current-buffer)))
  689.     (push (cons group buffer) nnml-nov-buffer-alist)
  690.     buffer)))
  691.  
  692. (defun nnml-save-nov ()
  693.   (save-excursion
  694.     (while nnml-nov-buffer-alist
  695.       (when (buffer-name (cdar nnml-nov-buffer-alist))
  696.     (set-buffer (cdar nnml-nov-buffer-alist))
  697.     (when (buffer-modified-p)
  698.       (nnmail-write-region 1 (point-max) (buffer-file-name) nil 'nomesg))
  699.     (set-buffer-modified-p nil)
  700.     (kill-buffer (current-buffer)))
  701.       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
  702.  
  703. ;;;###autoload
  704. (defun nnml-generate-nov-databases ()
  705.   "Generate NOV databases in all nnml directories."
  706.   (interactive)
  707.   ;; Read the active file to make sure we don't re-use articles
  708.   ;; numbers in empty groups.
  709.   (nnmail-activate 'nnml)
  710.   (nnml-open-server (or (nnoo-current-server 'nnml) ""))
  711.   (setq nnml-directory (expand-file-name nnml-directory))
  712.   ;; Recurse down the directories.
  713.   (nnml-generate-nov-databases-1 nnml-directory nil t)
  714.   ;; Save the active file.
  715.   (nnmail-save-active nnml-group-alist nnml-active-file))
  716.  
  717. (defun nnml-generate-nov-databases-1 (dir &optional seen no-active)
  718.   "Regenerate the NOV database in DIR."
  719.   (interactive "DRegenerate NOV in: ")
  720.   (setq dir (file-name-as-directory dir))
  721.   ;; Only scan this sub-tree if we haven't been here yet.
  722.   (unless (member (file-truename dir) seen)
  723.     (push (file-truename dir) seen)
  724.     ;; We descend recursively
  725.     (let ((dirs (directory-files dir t nil t))
  726.       dir)
  727.       (while (setq dir (pop dirs))
  728.     (when (and (not (member (file-name-nondirectory dir) '("." "..")))
  729.            (file-directory-p dir))
  730.       (nnml-generate-nov-databases-1 dir seen))))
  731.     ;; Do this directory.
  732.     (let ((files (sort (nnheader-article-to-file-alist dir)
  733.                (lambda (a b) (< (car a) (car b))))))
  734.       (when files
  735.     (funcall nnml-generate-active-function dir)
  736.     ;; Generate the nov file.
  737.     (nnml-generate-nov-file dir files)
  738.     (unless no-active
  739.       (nnmail-save-active nnml-group-alist nnml-active-file))))))
  740.  
  741. (defvar files)
  742. (defun nnml-generate-active-info (dir)
  743.   ;; Update the active info for this group.
  744.   (let ((group (nnheader-file-to-group
  745.         (directory-file-name dir) nnml-directory)))
  746.     (setq nnml-group-alist
  747.       (delq (assoc group nnml-group-alist) nnml-group-alist))
  748.     (push (list group
  749.         (cons (caar files)
  750.               (let ((f files))
  751.             (while (cdr f) (setq f (cdr f)))
  752.             (caar f))))
  753.       nnml-group-alist)))
  754.  
  755. (defun nnml-generate-nov-file (dir files)
  756.   (let* ((dir (file-name-as-directory dir))
  757.      (nov (concat dir nnml-nov-file-name))
  758.      (nov-buffer (get-buffer-create " *nov*"))
  759.      chars file headers)
  760.     (save-excursion
  761.       ;; Init the nov buffer.
  762.       (set-buffer nov-buffer)
  763.       (buffer-disable-undo (current-buffer))
  764.       (erase-buffer)
  765.       (set-buffer nntp-server-buffer)
  766.       ;; Delete the old NOV file.
  767.       (when (file-exists-p nov)
  768.     (funcall nnmail-delete-file-function nov))
  769.       (while files
  770.     (unless (file-directory-p (setq file (concat dir (cdar files))))
  771.       (erase-buffer)
  772.       (nnheader-insert-file-contents file)
  773.       (narrow-to-region
  774.        (goto-char (point-min))
  775.        (progn
  776.          (search-forward "\n\n" nil t)
  777.          (setq chars (- (point-max) (point)))
  778.          (max 1 (1- (point)))))
  779.       (unless (zerop (buffer-size))
  780.         (goto-char (point-min))
  781.         (setq headers (nnml-parse-head chars (caar files)))
  782.         (save-excursion
  783.           (set-buffer nov-buffer)
  784.           (goto-char (point-max))
  785.           (nnheader-insert-nov headers)))
  786.       (widen))
  787.     (setq files (cdr files)))
  788.       (save-excursion
  789.     (set-buffer nov-buffer)
  790.     (nnmail-write-region 1 (point-max) nov nil 'nomesg)
  791.     (kill-buffer (current-buffer))))))
  792.  
  793. (defun nnml-nov-delete-article (group article)
  794.   (save-excursion
  795.     (set-buffer (nnml-open-nov group))
  796.     (when (nnheader-find-nov-line article)
  797.       (delete-region (point) (progn (forward-line 1) (point)))
  798.       (when (bobp)
  799.     (let ((active (cadr (assoc group nnml-group-alist)))
  800.           num)
  801.       (when active
  802.         (if (eobp)
  803.         (setf (car active) (1+ (cdr active)))
  804.           (when (and (setq num (ignore-errors (read (current-buffer))))
  805.              (numberp num))
  806.         (setf (car active) num)))))))
  807.     t))
  808.  
  809. (defun nnml-update-file-alist (&optional force)
  810.   (when (or (not nnml-article-file-alist)
  811.         force)
  812.     (setq nnml-article-file-alist
  813.       (nnheader-article-to-file-alist nnml-current-directory))))
  814.  
  815. (provide 'nnml)
  816.  
  817. ;;; nnml.el ends here
  818.